package t20250428;

import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.TaskManagerOptions;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction;

import java.util.UUID;

public class StreamTestScale {

    public static void main(String[] args) throws Exception {


        Configuration flinkConf = new Configuration();
        flinkConf.setString("rest.port","9091");
        flinkConf.set(TaskManagerOptions.NUM_TASK_SLOTS, 10);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(flinkConf);


        DataStreamSource<String> source = env.addSource(new ParallelSourceFunction<String>() {


            private boolean flag = true;

            @Override
            public void run(SourceContext<String> ctx) throws Exception {
                while (flag) {
                    Thread.sleep(1000);
                    ctx.collect(UUID.randomUUID().toString());
                }
            }

            @Override
            public void cancel() {
                flag = false;
            }
        }).setParallelism(50);



        source.rescale().map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                return value + "---";
            }
        }).setParallelism(60).print().setParallelism(4);


//        String streamingPlanAsJSON = env.getStreamGraph().getStreamingPlanAsJSON();
//        System.out.println(streamingPlanAsJSON);
        env.execute("app");
    }
}
